package com.jinglvhuanjing.app.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import java.util.List;

public class DeviceUtil {

    private static String TAG = "DeviceUtil";

    /*隐藏虚拟按键*/
    public static boolean hideNavigation(Context context) {
        boolean ishide;
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib service call activity 42 s16 com.android.systemui";
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c", command});
            proc.waitFor();
            ishide = true;
        } catch (Exception ex) {
            LOG.e(TAG, ex.toString());
            Toast.makeText(context,
                    ex.getMessage(),
                    Toast.LENGTH_LONG).show();
            ishide = false;
        }
        return ishide;
    }

    /*显示虚拟按键*/
    public boolean showNavigation() {
        boolean isshow;
        try {
            String command;
            command = "LD_LIBRARY_PATH=/vendor/lib:/system/lib am startservice -n com.android.systemui/.SystemUIService";
            Process proc = Runtime.getRuntime().exec(new String[]{"su", "-c",
                    command});
            proc.waitFor();
            isshow = true;
        } catch (Exception e) {
            isshow = false;
            e.printStackTrace();
        }
        return isshow;
    }

    public static void setIccid(Context context) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
            SubscriptionManager sm = SubscriptionManager.from(context);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
            List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();
            if (sis.size() >= 1) {
                SubscriptionInfo si1 = sis.get(0);
                String iccId1 = si1.getIccId();
                Log.e(TAG, "iccId1="+iccId1);
                SpUtils.putString(context, Constants.ICCID, iccId1);
            }
            if (sis.size() >= 2) {
                SubscriptionInfo si2 = sis.get(1);
                String iccId2 = si2.getIccId();
                String phoneNum2 = si2.getNumber();
            }
        }
    }

}
